package com.example.demo.util.officeutil;


import com.alibaba.fastjson.JSON;
import com.example.demo.util.officeutil.dto.LineChartDTO;
import com.example.demo.util.officeutil.dto.XInfo;
import com.example.demo.util.officeutil.dto.YInfo;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class WordDemo {

    public static void main(String[] args) throws Exception {

        Map<String, Object> mockData = WordDemo.getMockData();

        // 读取word模板
        File file = new File("C:\\Users\\Administrator\\Desktop\\test\\2023第二季度_中心网络信息安全态势及机房硬件资源运行情况季报模板.docx");   //可改成你本地模板文件所在目录
        FileInputStream fileInputStream = new FileInputStream(file);
        WordTemplate template = new WordTemplate(fileInputStream);

        // 替换数据
        log.info("入参数据:\n" + JSON.toJSONString(mockData));
        template.replaceDocument(mockData);

        //生成文件
        File outputFile = new File("C:\\Users\\Administrator\\Desktop\\test\\2023第二季度_中心网络信息安全态势及机房硬件资源运行情况季报模板-new.docx");  //可改成你想要模板文件所在目录
        FileOutputStream fos = new FileOutputStream(outputFile);
        template.getDocument().write(fos);
    }


    public static Map<String, Object> getMockData() {

        Map<String, Object> parametersMap = new HashMap<>();
        List<Map<String, Object>> usageInfo = new ArrayList<>();

        //替换段落标签
        parametersMap.put("year", "2023");
        parametersMap.put("quarter", "三");

        Map<String, Object> usage1 = new HashMap<>();
        usage1.put("platformName", "某某某平台A");
        usage1.put("cpuTotal", "3438.50GHz");
        usage1.put("cpuPeakValueDate", "4月26日");
        usage1.put("cpuPeakValue", "168.17GHz");
        usage1.put("cpuPeakValueUseRate", "4.89%");
        usage1.put("memoryTotal", "8816.19GB");
        usage1.put("memoryPeakValueDate", "4月26日");
        usage1.put("memoryPeakValue", "100G");
        usage1.put("memoryPeakValueUseRate", "4.89%");
        usage1.put("storageTotal", "180TB");
        usage1.put("storageTotalUsed", "18TB");
        usage1.put("storageTotalRemain", "19TB");
        usageInfo.add(usage1);

        Map<String, Object> usage2 = new HashMap<>();
        usage2.put("platformName", "某某某平台B");
        usage2.put("cpuTotal", "22222.50GHz");
        usage2.put("cpuPeakValueDate", "4月26日");
        usage2.put("cpuPeakValue", "168.17GHz");
        usage2.put("cpuPeakValueUseRate", "4.89%");
        usage2.put("memoryTotal", "8816.19GB");
        usage2.put("memoryPeakValueDate", "4月26日");
        usage2.put("memoryPeakValue", "100G");
        usage2.put("memoryPeakValueUseRate", "4.89%");
        usage2.put("storageTotal", "180TB");
        usage2.put("storageTotalUsed", "18TB");
        usage2.put("storageTotalRemain", "19TB");
        usageInfo.add(usage2);
        parametersMap.put("usageInfo", usageInfo);


        XInfo xInfo = XInfo.builder().title("x标题").xAxisData(new String[]{"2021/01", "2021/02", "2021/03", "2021/04", "2021/05", "2021/06", "2021/07", "2021/08", "2021/09", "2021/10", "2021/11", "2021/12",}).build();
        YInfo yInfo = YInfo.builder().title("y标题").yAxisData(new Integer[]{10, 35, 21, 46, 79, 88, 39, 102, 71, 28, 99, 57}).build();
        LineChartDTO lineChart = LineChartDTO.builder().xInfo(xInfo).yInfo(yInfo).build();
        parametersMap.put("lineChart", lineChart);


        XInfo xInfo2 = XInfo.builder().title("x标题2").xAxisData(new String[]{"2021/01", "2021/02", "2021/03", "2021/04", "2021/05", "2021/06", "2021/07", "2021/08", "2021/09", "2021/10", "2021/11", "2021/12",}).build();
        YInfo yInfo2 = YInfo.builder().title("y标题2").yAxisData(new Integer[]{1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3}).build();
        LineChartDTO lineChart2 = LineChartDTO.builder().xInfo(xInfo2).yInfo(yInfo2).build();
        parametersMap.put("lineChart2", lineChart2);

        //表格数据
        List<Map<String, Object>> table1 = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("title", "系统名称" + i);
            map.put("vmCount", i);
            map.put("cpuTotal", 1000 + i);
            map.put("cpuTopUseRate", 10 + i + "%");
            map.put("memoryTotal", 10000 + i);
            map.put("memoryTopUseRate", 90 + i + "%");
            map.put("storageTotal", 7777 + i);
            map.put("storageUsed", 2222 + i);
            map.put("storageUsedUsedRate", 20 + i + "%");
            table1.add(map);
        }
        parametersMap.put("table1", table1);

        List<Map<String, Object>> table2 = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Map<String, Object> map = new HashMap<>();
            map.put("title", "2222系统名称" + i);
            map.put("vmCount", i);
            map.put("cpuTotal", 1000 + i);
            map.put("cpuTopUseRate", 10 + i + "%");
            map.put("memoryTotal", 10000 + i);
            map.put("memoryTopUseRate", 90 + i + "%");
            map.put("storageTotal", 7777 + i);
            map.put("storageUsed", 2222 + i);
            map.put("storageUsedUsedRate", 20 + i + "%");
            table2.add(map);
        }
        parametersMap.put("table2", table2);
        return parametersMap;
    }
}
